package com.wang.sort;

import java.util.Stack;
// (两个)栈结构实现队列结构
public class Demo14_twoStackImplementQueue {
    public class TwoStacksQueue {
        private Stack<Integer> stackPush;
        private Stack<Integer> stackPop;
        //初始化
        public TwoStacksQueue() {
            this.stackPush = new Stack<Integer>();
            this.stackPop = new Stack<Integer>();
        }
        //push栈向pop栈倒入数据
        private void pushToPop() {
            if(stackPop.isEmpty()) {
                while (! stackPush.empty()) {
                    stackPop.push(stackPush.pop());
                }
            }
        }
        private void add(int pushInt) {
            stackPush.push(pushInt);
            pushToPop();
        }
        private int poll() {
            if(stackPop.empty() && stackPush.empty()) {
                throw new RuntimeException("Queue is empty");
            }
            pushToPop();
            return stackPop.pop();
        }
        private int peek() {
            if(stackPop.empty() && stackPush.empty()) {
                throw new RuntimeException("Queue is empty");
            }
            pushToPop();
            return stackPop.peek();
        }
    }
}